package com.telecom.at.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.print.attribute.standard.PrinterLocation;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.model.AvgFlow;
import com.telecom.at.db.model.BWList;
import com.telecom.at.db.model.SameMac;
import com.telecom.at.db.model.SigBasAAA;
import com.telecom.at.db.model.SigData;
import com.telecom.at.db.model.User;

@WebServlet("/front/JsonListTop10")
public class JsonListTop10 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int date = Integer.parseInt(request.getParameter("date"));
        int date2 = Integer.parseInt(request.getParameter("date"));
        int startDate = date*10000+100;
		int endDate = date*10000+10100;
        //System.out.println(date);
        List<BWList>  bWList =Dac.getInstance().allBWList();
        Set<String> accountList=new HashSet<String>();
	       for(BWList bWList2:bWList)
	    	   accountList.add(bWList2.getAccount());
        List<SigBasAAA> sumSigBasAAAList = null;
        System.out.println(accountList);
        List<SameMac> macList = null;
		List<AvgFlow> flowList = null;
		List<AvgFlow> sumFlowList = null;
		List<SigData> sigList = null;
		
		
		//List<NumOfAccount> numList = null;
		User curUser = (User) request.getSession().getAttribute("curUser");
		List<AvgFlow> flowList2 =new ArrayList<AvgFlow>();
		List<AvgFlow> flowList3 =new ArrayList<AvgFlow>();
		List<SigData> sigList2 =new ArrayList<SigData>();
		List<SameMac> macList2 =new ArrayList<SameMac>();
       
        if (curUser.getId() == 0) {
        	macList = Dac.getInstance().getTopSameMacs();
        	sumFlowList = Dac.getInstance().getSumFlowOfDate(startDate,endDate);
        	flowList = Dac.getInstance().getAvgFlowOfDate(startDate,endDate);
            sumSigBasAAAList = Dac.getInstance().allSumSigBasAAA(date, date2);
            sigList = Dac.getInstance().getSigDataOfDate(date);
        } else {
        	macList = Dac.getInstance().getSameMacsByCity(curUser.getCity());
			flowList = Dac.getInstance().getAvgFlowByCityAndDate(startDate,endDate,curUser.getCity());
			sumFlowList = Dac.getInstance().getSumFlowByCityAndDate(startDate,endDate,curUser.getCity());
			sigList = Dac.getInstance().getSigDataByCityAndDate(date,curUser.getCity());
        }
        
        for(AvgFlow avgFlow:flowList) {
			if(!accountList.contains(avgFlow.getAccount())&&flowList2.size()<10) {
				
				flowList2.add(avgFlow);
			}
		}
        for(AvgFlow sumFlow:sumFlowList) {
			if(!accountList.contains(sumFlow.getAccount())&&flowList3.size()<10) {
				
				flowList3.add(sumFlow);
			}
		}
        for(SigData sigData:sigList) {
			if(!accountList.contains(sigData.getAccount())&&sigList2.size()<10) {
				
				sigList2.add(sigData);
			}
		}
        for(SameMac sameMac:macList) {
			if(macList2.size()<10) {
				
				macList2.add(sameMac);
			}
		}
        JSONArray jsonArray = new JSONArray(flowList2);
        JSONArray jsonArray2 = new JSONArray(sigList2);
        JSONArray jsonArray3 = new JSONArray(macList2);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", flowList2.size()+sigList2.size()+macList2.size());
        if(!flowList2.isEmpty()) {
        jsonObject.put("subjects", jsonArray);}
        else {
        	jsonObject.put("subjects", "1");
        }
        if(!sigList2.isEmpty()) {
        jsonObject.put("subjects1", jsonArray2);
        }else {
        	jsonObject.put("subjects1", "1");
        }
        if(!macList.isEmpty()) {
            jsonObject.put("subjects2", jsonArray3);
            }else {
            	jsonObject.put("subjects2", "1");
            }
        //System.out.println(jsonArray3);
        // System.out.println(jsonArray);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        //System.out.println(jsonObject.toString());
        response.getWriter().write(jsonObject.toString());
    }
}
